<html>
	<head>
		<title>Welcome - RIKAS documentation</title>
		<link rel="stylesheet" type="text/css" href="<?=base_url()?>style.css">
	</head>

	<body>
		<div id="content">
		<h1><a href="http://phpandi.blogspot.com">RIKAS DOCUMENTATION</a></h1>
		<h2 id="table-of-contents">Table of Contents</h2>
		<p>
			<ol>
				<li><a href="#installation">Installation</a></li>
				<li><a href="#hello-world">Hello World</a></li>
				<li><a href="#url">RIKAS' urls</a></li>
				<li><a href="#database">Working with a database</a></li>
				<li><a href="#helpers">Some Helper Functions</a></li>
			</ol>
		</p>
		<ol>
		<li><h2 id="installation">Installation</h2></li>
		<p>Installing and setting up RIKAS is as easy as 1-2-3
		<ol>
			<li>Extract RIKAS to your webserver directory</li>
			<p>Extract RIKAS into a folder in your webserver. You can extract to your
			root directory or other subdirectories in your webserver.
			</p>
			<div class="example-name"><tt>Example 1. (extract to the webroot)</tt></div>
			<pre class="code">
/-------+	<--webroot folder
	|
	config/
	controllers/
	docs/
	lib/
	models/
	views/
	index.php
	style.css</pre>			
			<div class="example-name"><tt>Example 2. (extract to a subfolder)</tt></div>
			<pre class="code">
/-------+	<--webroot folder
	|
	rikas/----------+	<--subfolder
			|
			config/
			controllers/
			docs/
			lib/
			models/
			views/
			index.php
			style.css</pre>			
			<li>Modify the configuration file</li>
			<p>
			Modify the $config['base_url'] according to the way you extracted the
			rikas application.
			<div class="example-name"><tt>Example 1. (extracted to the webroot)</tt></div>
			<pre class="code">
$config['base_url'] = 'http://localhost/';
</pre>
			<div class="example-name"><tt>Example 2. (extracted to a subfolder)</tt></div>
			<pre class="code">
$config['base_url'] = 'http://localhost/rikas';
</pre>
			
			</p>
			<li>Modify the index.php </li>
			<p>RIKAS is developed under Linux, however windows users can also use the framework.
			If your server is linux, you can skip this part, (that would be -1 step for
			linux users). For windows users you can simply edit index.php and locate the first 
			two lines. Uncomment the DIR_SEP which is intended for windows and remove the line 
			which is intended for linux. Window users should have something that looks like 
			this.
			</p>
			<pre class="code">
&lt;?php
	//define('DIR_SEP','/');
	define('DIR_SEP','\\\\');</pre>
		</ol>
		</p>
		<li><h2 id="hello-world">Hello World</h2></li>
		<p>
			Create your very first "hello world"
			application using the <a href="http://phpandi.blogspot.com">RIKAS framework</a>.
		</p>
		

<div class="filename"><tt>hello.php</tt></div>
		<pre class="code">
&lt;?php
class Hello extends Controller {
	function Hello() {
		parent::Controller();
	}
	function index() {
		echo 'hello rikas world';
	}
}
?></pre>
		<p>
			Copy the script above and put it under controllers/ folder. After creating the script, 
			if all goes well, you can access the application by navigating to the url given below:
		</p>
		<div class="action-url"><tt>http://localhost/rikas/index.php/hello/index</tt></div>
		<p> This should should take you to a script greeting you a hello world. If not then you might
		have missed something, better go over the steps again.
		</p>
		<h4> Dissect the hello world application</h4>
		<p>
			Let's now dissect the "hello world" application. 
			The first thing that we did was to declare a class named Hello which 
			inherits member/methods from the base classController, 
			take note that class names should, by convention 
			start with a capital letter and the filename should all	be in lowercase. The member 
			function index() is the method which prints out the "hello world" message.
		</p>	<li><h2 id="url">RIKAS' urls </h2></li>
	<p>
		<a href="http://phpandi.blogspot.com"><em>RIKAS</em></a> uses segmented urls instead of query strings for easy search engine optimization.
	</p>
	<pre>
		/*
		 *@todo provide 
		 */
	</pre>
	
	<li><h2 id="database">Working with the database</h2></li>
		<p>
			<ul>
				<li>
					<h3>Connecting to a database</h3>
					<p>
					It is recommended to access the database via
					the models. Once you extend the Model class, the child class
					automatically inherits the $db member variable, which is an
					instance of the Database class and is automatically connected
					to the database via the database variables given in the config
					file. However there are also instances where you may need the database
					connection in the controller. To connect to the database within
					your controllers simply create an instance of the database class.
					</p>
					<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name');
?></pre>
				</li>
				<li>
					<h3>Querying the database</h3>
					<p>
					Querying the database is very simple and straight-forward in 
					<a href="http://phpandi.blogspot.com"></em>RIKAS</em></a>.
					</p>

<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$db->query('SELECT * FROM `table_name`);
?></pre>
					<p>
					Running the code above will query the database. The <em>query()</em>
					function returns a MYSQL_RESULT object on success and false if there
					is an error.
					</p>					
									</li>
				<li>
					<h3>Fetching the query results</h3>
					<p>
					Fetching query results has never been easier. This is best described
					by an example code:
					</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$res = $db->query('SELECT * FROM `table_name`);
$result = $res->resultArray();
?></pre>
					<p>
					The function </em>resultArray()</em> will return the result set as an associative
					array.
					</p>
					<p>
					Aside from the common query method shown above, you can also perform
					a query in this fashion.
					</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$res = $db->query('SELECT * FROM `table_name` where id=?', array(1));
$result = $res->resultArray();
?></pre>
					
				</li>
				<li>
					<h3>Some database utility functions/methods</h3>
					<h4>Utility functions/methods from under database object</h4>
					<ul>
						<li><strong><em>insert(string $table, array $data=array(), string $condition=false)</em></strong>
							<p>takes an associative array and inserts it into table
							$table. Optionally takes an argument $condition, which
							can be used when update instead of inserting.
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$new_name = $array('id'=>1, 'name'=>'erwin');
$db->insert('names', $new_name);
?></pre>
						</li>
						<li><strong><em>limit(int $page=0, int $numPerPage=0)</em></strong>
							<p>returns a computed limit clause based on the page number
							$page and number of results per page $numPerPage.
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM 'table_name'";
$sql = Database::limit(1,10);
$db->query($sql);
?></pre>
						</li>
						

						<li><strong><em>lastInsertId(void)</em></strong>
							<p>returns the last inserted id.
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$new_name = $array('id'=>1, 'name'=>'erwin');
$db->insert('names', $new_name);
echo $db->lastInsertId();
?></pre>
						</li>

						<li><strong><em>$err</em></strong>
							<p>a variable containing the last encountered 
							database error.
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$res = $db->query('SELECT ' FROM `table_name`);
echo $db->err;
?></pre>
						</li>
						
						
						
					</ul>
					<h4>Utility functions/methods from under database result object</h4>
					<ul>
						<li><strong><em>resultArray(void)</em></strong>
							<p>returns the result set as an array of rows.
							the result rows are always associative arrays. 
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$res = $db->query('SELECT * FROM `table_name`);
$result = $res->resultArray();
print_r($result);
?></pre>
						</li>

						<li><strong><em>rowArray(void)</em></strong>
							<p>returns a single row as an associative array.
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$res = $db->query('SELECT * FROM `table_name`);
$result = $res->resultRow();
print_r($result);
?></pre>
						</li>

						<li><strong><em>numRows(void)</em></strong>
							<p>returns the total number of rows in the
							result set.
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$res = $db->query('SELECT * FROM `table_name`);
echo $res->numRows());
?></pre>
						</li>
						<li><strong><em>affectedRows(void)</em></strong>
							<p>returns the total number of affected rows in a
							delete,update...etc.. query.
							</p>
<pre class="code">
&lt;?php
$db = new Database('host','username','password','database_name);
$res = $db->query('DELETE FROM `table_name`);
echo $res->affectedRows();
?></pre>
						</li>
					</ul>
					
				</li>
			</ul>
		</p>
	<li><h2 id="helpers">Some helper functions</h2></li>
		<p>
			<ul>
				<li>
					<strong><em>base_url( void )</em></strong>
					<p>
					Returns the base url. This is the <em>$config['base_url']</em> value.
					</p>
					<pre class="code">
&lt;?php
	echo base_url();
?></pre>
					<div class="example-name"><tt>output:</tt></div>
					<pre class="output">
<?=base_url()?></pre>
				</li>
				<li>
					<strong><em>url ( string $str, array $args=array() )</em></strong>
					<p>
					Automagically creates a url given the controller/method $str and
					other parameters $args.
					</p>
					<pre class="code">
&lt;?php
	echo url('doc/index', array('erwin',1,2));
?></pre>
					<div class="example-name"><tt>output:</tt></div>
					<pre class="output">
<?=url('doc/index', array('erwin',1,2))?></pre>
					
				</li>
				<li>
					<strong><em>redirect(string $string, array $args=array())</em></strong>
					<p>
					performs a redirect to a page whose url is created by the <em>url()</em> function.
					</p>
				</li>
				<li>
					<strong><em>mdEncrypt(string $str)</em></strong>
					<p> Encrypts the given string $str using the md5 encryption 
						algorithm.
					<pre class="code">
&lt;?php
	mdEncrypt('RIKAS');
?></pre>
					<div class="example-name">output:</div>
					<pre class="output">
<?=mdEncrypt('RIKAS');?></pre>
					</p>
				</li>
			</ul>
		</p>
	</ol>
	</div>
	</body>
</html>
